package com.lzy.huijinjiazu.mvp.ui.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.di.component.DaggerExtravagantMallComponent;
import com.lzy.huijinjiazu.di.module.ExtravagantMallModule;
import com.lzy.huijinjiazu.mvp.contract.ExtravagantMallContract;
import com.lzy.huijinjiazu.mvp.presenter.ExtravagantMallPresenter;
import com.lzy.huijinjiazu.mvp.ui.activity.SearchActivity;
import com.lzy.huijinjiazu.mvp.ui.adapter.ExtravagantClassifAdapter;
import com.lzy.huijinjiazu.mvp.ui.adapter.GoodsAdapter;
import com.lzy.huijinjiazu.mvp.ui.widget.BannerGridLoader;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

//奢侈品商城
public class ExtravagantMallFragment extends BaseFragment<ExtravagantMallPresenter> implements ExtravagantMallContract.View {

    @BindView(R.id.banner)
    Banner mBanner;
    @BindView(R.id.rv_classif)
    RecyclerView mRvClassif;
    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.tv_content_love1)
    TextView mTvContentLove1;
    private GoodsAdapter mAdapter;
    private ExtravagantClassifAdapter mClassAdapter;

    public static ExtravagantMallFragment newInstance() {
        ExtravagantMallFragment fragment = new ExtravagantMallFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerExtravagantMallComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .extravagantMallModule(new ExtravagantMallModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_extravagant_mall, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTvContentLove1.setText(Html.fromHtml(ArmsUtils.getString(getContext(), R.string.symbol_moeny, "47500")));
        initRecyclerView();
        initBannerData();
        classifData();
        goodsData();
    }

    //初始化recyclerview
    private void initRecyclerView() {
        ArmsUtils.configRecyclerView(mRvList, new GridLayoutManager(getContext(), 2) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRvList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
                super.getItemOffsets(outRect, itemPosition, parent);
                outRect.right = (itemPosition + 1) % 2 == 0 ? 0 : ArmsUtils.dip2px(getContext(), 2.5f);
                outRect.left = (itemPosition + 1) % 2 == 0 ? ArmsUtils.dip2px(getContext(), 2.5f) : 0;
                outRect.bottom = ArmsUtils.dip2px(getContext(), 5);
            }
        });
        mAdapter = new GoodsAdapter();
        mRvList.setAdapter(mAdapter);
        ArmsUtils.configRecyclerView(mRvClassif, new GridLayoutManager(getContext(), 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mClassAdapter = new ExtravagantClassifAdapter();
        mRvClassif.setAdapter(mClassAdapter);
    }

    //banner初始化数据
    private void initBannerData() {
        List<String> imgs = new ArrayList<>();
        imgs.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1515402991633&di=56910be1b51e3110d2879ea17533aa35&imgtype=0&src=http%3A%2F%2Fpic29.nipic.com%2F20130531%2F12167069_094833224366_2.jpg");
        imgs.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1515403009251&di=101f55dadbc03150cba999fe98d3bf72&imgtype=0&src=http%3A%2F%2Fimg01.tooopen.com%2Fdowns%2Fimages%2F2010%2F8%2F10%2Fsy_20100810102305425427.jpg");
        imgs.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1515403033784&di=4672a8df2c8cf0102d2e9bb70682784e&imgtype=0&src=http%3A%2F%2Fscimg.jb51.net%2Fallimg%2F140808%2F11-140PQ50242311.jpg");
        mBanner.setImages(imgs)
                .setImageLoader(new BannerGridLoader())
                .setOnBannerListener(index -> {

                })
                .start();
    }

    private void classifData() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            data.add("");
        }
        mClassAdapter.setNewData(data);
    }

    private void goodsData() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            data.add("");
        }
        mAdapter.setNewData(data);
    }

    @OnClick(R.id.btn_search)
    public void onViewClicked() {
        ArmsUtils.startActivity(SearchActivity.class);
    }

}
